<?php 

namespace Home\Controller;
use Think\Controller;

/**
* 
*/
class UserCenterController extends CommonController
{
	
	public function index(){
		$question = M('question')->where(array('uid'=>$_SESSION['uid']))->order('create_time desc')->select();
		$pub_num = count($question);
		$nid = $_SESSION['uid'];
		//var_dump($count);exit;
		$sql = "select t1.title,t1.qid,t2.create_time from question t1 left join collect t2 on t1.qid=t2.qid where t2.uid=$nid order by create_time desc";
		$collect = M()->query($sql);
		$col_num = count($collect);

		//var_dump($collect);exit;

		$this->assign('pub_num',$pub_num);
		$this->assign('col_num',$col_num);
		$this->assign('collect',$collect);
		$this->assign('question',$question);
		$this->assign('title','用户中心');
		$this -> display();
	}

	public function Set(){
		$user = M('user')->find($_SESSION['uid']);
		//var_dump($user);exit;
		$this->assign('user',$user);
		$this->assign('title','设置');
		$this -> display();

	}

	/*public function sendMail(){
		$content = <<<
		xbsmail('2827297270@qq.com','测试邮件','这是测试邮件<a href="www.baidu.com">www.baidu.com</a>');
	}*/

	public function Msg(){
		$uid = $_SESSION['uid'];
		$sql = "select t1.aid,t1.uid,t1.create_time,t2.nickname,t3.qid,t3.title from answer t1 left join user t2 on t1.uid=t2.uid left join question t3 on t1.qid=t3.qid where t3.uid='{$uid}' and t1.is_read='0' order by t1.create_time desc";
		$msgs = M()->query($sql);
		$count = count($msgs);
		$this->assign('count',$count);

		$Page = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show = $Page->show();// 分页显示输出

		$sql_page = "select t1.aid,t1.uid,t1.create_time,t2.nickname,t3.qid,t3.title from answer t1 left join user t2 on t1.uid=t2.uid left join question t3 on t1.qid=t3.qid where t3.uid='{$uid}' and t1.is_read='0' order by t1.create_time desc limit ".$Page->firstRow.",".$Page->listRows;
		$msg = M()->query($sql_page);
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$this->assign('msg',$msg);
		$this->assign('list',$list);// 赋值数据集
		$this->assign('show',$show);// 赋值分页输出

		$this->assign('title','我的消息');
		$this -> display();
	}

	public function delMsg(){
		$uid = $_SESSION['uid'];/*
		echo '<script>alert('.I('get.del').')</script>';exit;*/
		if($aid = I('get.aid')){
			M('answer')->where(['aid'=>$aid])->setField(['is_read'=>1]);
			if($_SESSION['msg']>0){
				$_SESSION['msg'] = $_SESSION['msg']-1;
				M('user')->where(['uid'=>$uid])->setDec('msg');
			}
		}elseif(I('get.del')==1){
			$_SESSION['msg'] = 0;
			M('user')->where(['uid'=>$uid])->setField(['msg'=>0]);
		}elseif(I('get.del')==2){
			$sql = "update answer t1 left join question t2 on t1.qid=t2.qid set t1.is_read='1' where t2.uid='{$uid}' and t1.is_read='0'";
			M('answer')->execute($sql);
			/*$_SESSION['msg'] = 0;
			M('user')->where(['uid'=>$uid])->setField(['msg'=>0]);*/
		}
		header('location:'.U('home/userCenter/msg'));
	}

	public function Can(){
		$this->assign('title','申请授权');
		$this -> display();
	}

	public function Center(){
		//checklog();
		$user;
		$id=I('get.id');
		$question = M('question');
		$answer = M('answer');
		
		if($id){
			$sql = "select t1.create_time,t1.content,t1.qid,t2.title from answer t1 left join question t2 on t1.qid=t2.qid where t1.uid='{$id}' order by create_time desc limit 15";
			$res=M()->query($sql);
			//var_dump($res);exit;
			$user=M('user')->find($id);
			$que=$question->field('title,answer_num,view_num,create_time,qid')->where(['uid'=>$id])->order('create_time desc')->limit(30)->select();
			//var_dump($que);exit;
		}elseif($_SESSION['uid']){
			$sql = "select t1.create_time,t1.content,t2.title from answer t1 left join question t2 on t1.qid=t2.qid where t1.uid='{$_SESSION['uid']}' order by create_time desc limit 15";
			$res=M()->query($sql);
			//var_dump($res);exit;
			$user=M('user')->find($_SESSION['uid']);
			$que=$question->field('title,answer_num,view_num,create_time,qid')->where(['uid'=>$_SESSION['uid']])->order('create_time desc')->limit(30)->select();
			//var_dump($que);exit;
		}
		
		if(!$user){
        	$this->error("该用户不存在",U('home/index/index'));
        }
        $this->assign('res',$res);
        $this->assign('que',$que);
		$this->assign('user',$user);
		$this->assign('title',$user['nickname'].'的主页');
		$this -> display();
	}

	public function setData(){
		$data = I('post.');
		$user = D('User');
		//var_dump(isset($data));exit;
		//var_dump($_SERVER);exit;
		if(isset($data['password'])){
			if(!$user->create($data,3)){
				$this->ajaxReturn(["error"=>1,"info"=>$user->getError()]);
			}

			if (isset($_SESSION['uid'])) {
				if($_SESSION['password']!=md5($data['nowpass'])){
					$this->ajaxReturn(['error'=>1,'info'=>'当前密码错误']);
				}else{
					$user->where(array('uid'=>$_SESSION['uid']))->setField('password',md5($data['password']));
					$this->ajaxReturn(['error'=>0,'info'=>'修改成功']);
				}
			}
		}elseif(isset($data['email'])){
			$data['sign']=trim($data['sign']);
			if(!$user->create($data,5)){
				$this->ajaxReturn(["error"=>1,"info"=>$user->getError()]);
			}
			
			$resE=M('user')->where(['email'=>$data['email'],'uid'=>['neq',$_SESSION['uid']]])->select();
			if($resE){
				$this->ajaxReturn(['error'=>1,'info'=>"邮箱已存在"]);
			};
			$resE=M('user')->where(['nickname'=>$data['nickname'],'uid'=>['neq',$_SESSION['uid']]])->select();
			if($resE){
				$this->ajaxReturn(['error'=>1,'info'=>"昵称已存在"]);
			};

			/*if($data['email']!=$_SESSION['email'] && $data['nickname']!=$_SESSION['nickname']){
				if(!$user->create($data,6)){
					$this->ajaxReturn(["error"=>1,"info"=>$user->getError()]);
				}
			}elseif($data['email']==$_SESSION['email'] && $data['nickname']!=$_SESSION['nickname']){
				if(!$user->create($data,7)){
					$this->ajaxReturn(["error"=>1,"info"=>$user->getError()]);
				}
			}elseif($data['nickname']==$_SESSION['nickname'] && $data['email']!=$_SESSION['email']){
				if(!$user->create($data,8)){
					$this->ajaxReturn(["error"=>1,"info"=>$user->getError()]);
				}
			}*/
			if (isset($_SESSION['uid'])){

				$user->where(array('uid'=>$_SESSION['uid']))->setField($data);
				/*$_SESSION['sex']=$data['sex'];
				$_SESSION['nickname']=$data['nickname'];
				$_SESSION['email']=$data['email'];
				$_SESSION['city']=$data['city'];
				$_SESSION['sign']=$data['sign'];*/
				$this->ajaxReturn(['error'=>0,'info'=>'修改成功']);
			}
		}
	}
	public function upload(){
		//var_dump($_FILES);exit;
		$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();
	    if(!$info) {// 上传错误提示错误信息
	        //$this->error($upload->getError());
	       echo '失败';
	    }else{
	    	// 上传成功
	    	$face = 'Uploads/'.$info['face']['savepath'].$info['face']['savename'];

	    	M('user')->where(['uid'=>$_SESSION['uid']])->save(['face'=>$face]);

	    	unlink($_SESSION['face']);

	    	$_SESSION['face'] = $face;

	    	echo '成功';
	        //$this->success('上传成功！');
	      /*  $path=
	        $user = M('user');
	        $user -> */
	    }
	}
}


 ?>